package com.have.blog.xo.service.admin.impl;


import com.have.blog.base.enums.BlogPublishStatus;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.model.mapper.TSysDictDataMapper;
import com.have.blog.model.mapper.TSysDictTypeMapper;
import com.have.blog.model.models.TSysDictData;
import com.have.blog.model.models.TSysDictDataExample;
import com.have.blog.model.models.TSysDictType;
import com.have.blog.model.models.TSysDictTypeExample;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.constant.SysConstant;
import com.have.blog.xo.service.admin.SystemDictDataService;
import com.have.blog.xo.service.cache.AdminSysDictDataCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@SuppressWarnings("all")
public class SystemDictDataServiceImpl implements SystemDictDataService {
    @Autowired
    private TSysDictDataMapper dictDataMapper;
    @Autowired
    private TSysDictTypeMapper dictTypeMapper;
    @Autowired
    private AdminSysDictDataCacheService dictDataCacheService;

    @Override
    public String getListByDictType(String dictType) {
        Map<String, Object> resultMap;

        resultMap = dictDataCacheService.getDictData(dictType);
        if (!CollectionUtils.isEmpty(resultMap)) {
            return ResultUtils.result(SysConstant.SUCCESS, resultMap);
        }

        TSysDictTypeExample typeExample = new TSysDictTypeExample();
        typeExample.createCriteria()
                .andDictTypeEqualTo(dictType)
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andIsPublishEqualTo(BlogPublishStatus.PUBLISH.getStatus());
        List<TSysDictType> tSysDictTypes = dictTypeMapper.selectByExample(typeExample);
        if (CollectionUtils.isEmpty(tSysDictTypes)) {
            return ResultUtils.result(SysConstant.ERROR, MessageConstant.OPERATION_FAIL);
        }
        TSysDictType sysDictTyp = tSysDictTypes.get(0);
        System.out.println(sysDictTyp);

        TSysDictDataExample dataExample = new TSysDictDataExample();
        dataExample.createCriteria()
                .andIsPublishEqualTo(BlogPublishStatus.PUBLISH.getStatus())
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andDictTypeUidEqualTo(sysDictTyp.getUid());
        dataExample.setOrderByClause("create_time desc");
        List<TSysDictData> dictDataList = dictDataMapper.selectByExample(dataExample);

        String defaultValue = null;
        for (TSysDictData dictData : dictDataList) {
            if (dictData.getIsDefault() == SysConstant.ONE) {
                defaultValue = dictData.getDictValue();
                break;
            }
        }

        resultMap = new HashMap<>();
        resultMap.put(SysConstant.DEFAULT_VALUE, defaultValue);
        resultMap.put(SysConstant.LIST, dictDataList);
        dictDataCacheService.saveDictData(resultMap, dictType, 1, TimeUnit.DAYS);
        return ResultUtils.result(SysConstant.SUCCESS, resultMap);
    }

    @Override
    public String getDataListByDictTypeList(List<String> dictTypeList) {

        Map<String, Map<String, Object>> resultMap = new HashMap<>();
        List<String> tempTypeList = new ArrayList<>();
        dictTypeList.forEach(item -> {
            Map<String, Object> tempMap = dictDataCacheService.getDataListByDictType(item);
            if (!CollectionUtils.isEmpty(tempMap)) {
                resultMap.put(item, tempMap);
            } else {
                tempTypeList.add(item);
            }
        });

        if (CollectionUtils.isEmpty(tempTypeList)) {
            return ResultUtils.successWithData(resultMap);
        }
        TSysDictTypeExample typeExample = new TSysDictTypeExample();
        typeExample.createCriteria()
                .andDictTypeIn(tempTypeList)
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andIsPublishEqualTo(BlogPublishStatus.PUBLISH.getStatus());
        List<TSysDictType> sysDictTypeList = dictTypeMapper.selectByExample(typeExample);
        sysDictTypeList.forEach(item -> {
            TSysDictDataExample dataExample = new TSysDictDataExample();
            dataExample.createCriteria()
                    .andIsPublishEqualTo(BlogPublishStatus.PUBLISH.getStatus())
                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                    .andDictTypeUidEqualTo(item.getUid());
            dataExample.setOrderByClause("sort,create_time desc");
            List<TSysDictData> list = dictDataMapper.selectByExample(dataExample);
            String defaultValue = null;
            for (TSysDictData sysDictData : list) {
                if (sysDictData.getIsDefault() == SysConstant.ONE) {
                    defaultValue = sysDictData.getDictValue();
                    break;
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put(SysConstant.DEFAULT_VALUE, defaultValue);
            map.put(SysConstant.LIST, list);
            resultMap.put(item.getDictType(), map);
            dictDataCacheService.saveDataListByDictType(map, item.getDictType(), 1, TimeUnit.DAYS);
        });
        return ResultUtils.successWithData(resultMap);
    }
}
